package cn.darkfire.web.Service.impl;

import cn.darkfire.web.Dao.UserDao;
import cn.darkfire.web.Dao.imp.UserDaoImpl;
import cn.darkfire.web.POJO.LoginUser;
import cn.darkfire.web.POJO.Status;
import cn.darkfire.web.Service.UserService;
import cn.darkfire.web.Utils.DataUtils;
import cn.darkfire.web.Utils.EmailUtils;

import java.util.List;

public class UserServiceImpl implements UserService {
    private static final UserDao userDao = new UserDaoImpl();

    @Override
    public Status login(LoginUser user) {
        LoginUser user_ = userDao.selectByName(user.name);
        String password_md5 = DataUtils.getInstance().stringToMD5(user.password).toUpperCase();
        System.out.println("md5HEx->"+password_md5);
        if(user_ == null){
            return new Status(-1,"fail");
        }else if(user_.password.equals(password_md5)){
            //登录成功
            return new Status(200,"ok");
        }else{
            //登录失败
            return new Status(-1,"fail");
        }
    }

    @Override
    public Status register(LoginUser user) {
      if( userDao.insert(user)==0){
          return new Status(500,"创建用户失败！");
      }else{
          return new Status(200,"创建用户成功！");
      }
    }

    @Override
    public Status changePassword(LoginUser user) {
        if(userDao.update(user)>=1){
            return new Status(200,"修改用户成功！");
        }else{
            return new Status(200,"修改用户失败！");
        }
    }

    @Override
    public Status removeUser(String username) {
        if(userDao.delete(username)==0){
            return new Status(200,"删除用户成功！");
        }else{
            return new Status(200,"删除用户失败！");
        }
    }

    @Override
    public List<LoginUser> getAllUser() {
        return userDao.select();
    }

    @Override
    public List<LoginUser> searchUserByName(String username) {
        return userDao.selectAllByName(username);
    }

    @Override
    public LoginUser searchUserByNameTrue(String username) {
        return userDao.selectByName(username);
    }

    @Override
    public boolean isReg(String username) {
        try{
            String name = userDao.selectByName(username).getName();
            if(name == null || name.equals("")){
                //表示未注册
                return false;
            }
            return true;
        }catch (Exception e){
            return false;
        }

    }

    @Override
    public Status sendEmail(String username, String email) {
       //检测用户是否存在,存在则发送邮箱
        if(username.length()<= 1 || EmailUtils.getInstance().isEmail(email)){
            Status.getInstance().setStatus(-1,"邮箱或用户名输入错误！");
            return Status.getInstance();
        }

        if(userDao.selectByName(username)==null){
            Status.getInstance().setStatus(-1,"用户不存在！");
            return Status.getInstance();
        }else{
            //此时发送email
            //if(user.eamil.equals(eamil)){} //验证邮箱
            String code = EmailUtils.getInstance().sendEmail(email);
            if(code.equals("-1")){
                Status.getInstance().setStatus(-1,"邮件发送失败");
                return Status.getInstance();
            }else{
                Status.getInstance().setStatus(200,code);
                return Status.getInstance();
            }
        }
    }
}
